<?php

namespace app\admin\controller;

use think\Db;
use think\Session;

class Admin extends Authorized
{

    public function index(){return $this->fetch();}
    public function xs(){
        $role_id=2;
        $this->assign('role_id', $role_id);
        return $this->fetch();
    }
    public function seo(){
        $role_id=1;
        $this->assign('role_id', $role_id);
        return $this->fetch();
    }

    public function getList()
    {
        $get_data = $this->request->get();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $role_id = input('role_id');
        $keywords = input('keywords');
        $where = [];
        $where['a.id']=['>',1];
        $where['a.user_id']=0;

        if (!empty($keywords)) {
            $where['a.username'] = ['like', '%' . $keywords . '%'];
        }
        if (!empty($role_id)) {
            $where['a.role_id'] =$role_id;
        }

        $count = Db::table('admin a')->field('a.*,r.title role_title,r.login')->join('role r', 'a.role_id=r.id', 'left')
            ->where($where)
            ->count();
        $rows = Db::table('admin a')->field('a.*,r.title role_title,r.login')->join('role r', 'a.role_id=r.id', 'left')
            ->where($where)
            ->order('a.state,a.id desc')
            ->page($page, $limit)
            ->select();

        return json(['code' => 0, 'count' => $count, 'msg' =>'', 'data' => $rows]);
    }

    public function add()
    {
        $post_data = $this->request->post();
        if ($post_data) {
            $post_data['password'] = md5(config('default_pwd'));
            Db::table('admin')->startTrans();

            $id_nums = Db::table('admin')->where(['username'=>$post_data['username'],'role_id'=>$post_data['role_id'],])->find();
            if($id_nums){
                return json(['code' => 1, 'count' => 0, 'msg' => '添加失败,已存在相同信息', 'data' => '']);
            }

            $id = Db::table('admin')->insertGetId($post_data);
            if (false === $id) {
                Db::table('admin')->rollback();
                return json(['code' => 1, 'count' => 0, 'msg' => '添加失败', 'data' => '']);
            } else {
                Db::table('admin')->commit();
                return json(['code' => 0, 'count' => 0, 'msg' => '添加成功', 'data' => '']);
            }
        }

        $role_id = input('role_id');
        $where = [];
        if (!empty($role_id)) {
            $where['id'] =$role_id;
        }

        $role=Db::table('role')->where($where)->select();
        $this->assign('role', $role);
        return $this->fetch();
    }

    public function reset()
    {
        $post_data = $this->request->post();
        if ($post_data) {
            $post_data['update_time']=date('Y-m-d H:i:s',time());
            $post_data['password'] = md5(config('default_pwd'));
            $result = Db::table('admin')->update($post_data);
            if (false === $result) {
                return json(['msg' => '失败', 'code' => 1]);
            } else {
                return json(['msg' => '成功', 'code' => 0]);
            }
        }
    }

//    状态变更
    public function state()
    {
        $post_data = $this->request->post();
        if ($post_data) {
            $post_data['update_time']=date('Y-m-d H:i:s',time());
            $result = Db::table('admin')->update($post_data);
            if (false === $result) {
                return json(['msg' => '失败', 'code' => 1]);
            } else {
                return json(['msg' => '成功', 'code' => 0]);
            }
        }
    }

    public function edit()
    {
        $post_data = $this->request->post();
        if ($post_data) {
            $post_data['update_time']=date('Y-m-d H:i:s',time());
            $result = Db::table('admin')->update($post_data);
            if (false === $result) {
                return json(['msg' => '失败', 'code' => 1]);
            } else {
                return json(['msg' => '成功', 'code' => 0]);
            }
        }


        $article = Db::table('admin')->where('id', input('id'))->find();
        $this->assign('article', $article);

        $role=Db::table('role')->select();
        $this->assign('role', $role);

        return $this->fetch();
    }


    public function del()
    {
        $post_data = $this->request->post();
        if ($post_data) {
            $result = Db::table('admin')->where('id', $post_data['id'])->delete();
            if (false === $result) {
                return json(['msg' => '删除失败', 'code' => 1]);
            } else {
                return json(['msg' => '删除成功', 'code' => 0]);
            }
        }
    }
}